#!/bin/sh

# A simple script to change wallpaper in wayland
systemctl --user import-environment DISPLAY WAYLAND_DISPLAY

random_wall=$(shuf -n 1 -e ~/pix/wall/dark/*)
uwall="$HOME/pix/wall/nature/unsplash-$(date +"%d-%m-%H-%M-%S").jpg"
url="https://source.unsplash.com/random"
resolution=1920x1080
keywords="?4k,mountains,lakes,seashore,river,night,dark"
custom_wall=/tmp/custom-$(date "+%d-%m-%H-%M-%S").jpg

background() {
    convert $random_wall -quality 100 -gravity Center -resize 1920x1080+0+0! $custom_wall
}

unsplash_wall() {
    curl -fsSL $url/$resolution/$keywords -o $uwall
    pgrep swaybg && pkill swaybg
    swaybg -m fill -i $uwall
}



options="$1"

if [ "$options" = "lock" ]; then
    background && swaylock -f -i $custom_wall
elif [ "$options" = "wall" ]; then
    pgrep swaybg && pkill swaybg
    #background && swaybg -m fill -i $custom_wall
    # TODO: use identify command on image, if its > 1920x1080, no crop.
    # Comment next line if you don't want to crop (A little bit faster)
    swaybg -m fill -i $random_wall
    WAYLAND_DISPLAY=wayland-1 wl-copy -o $random_wall
elif [ "$options" = "uwall" ]; then
    unsplash_wall
else
    exit 1
fi

# vim:ft=sh
